import os
import pprint
import datetime
import cPickle

os.environ['DJANGO_SETTINGS_MODULE'] = 'ceemtu.settings'

from ceemtu.cetcore.models import *
from django.contrib.auth.models import User

import unittest

class testPrimitiveArchive(unittest.TestCase):
    """
    A test class for the primitive archive.
    """

    def setUp(self):
        self.foo = 1

    def tearDown(self):
        pass

    def testFoo(self):
        try:
            # Create users
            user_fred = User.objects.create_user(
                username = 'fred',
                email = 'fred@weholt.org',
                password = 'dummy'
                )
        
            user_cecilie = User.objects.create_user(
                username = 'cecilie',
                email = 'cecilie@weholt.org',
                password = 'dummy'
                )
            
            archive1 = Archive(
                user = user_fred,
                name = 'test_archive1',
                path = '/some/place/',
                )    
            archive1.save()
            
            file1 = FileEntry(
                archive = archive1,
                filepath = '/some/alltid med',
                filename = 'test1',
                extension = 'jpg',
                filesize = 1000,
                created_datetime = datetime.datetime.today(),
                modified_datetime = datetime.datetime.today(),
                authentication_required = False,
                mature_content = False,
                personal_content = False,
                in_the_attic = False
            )
            file1.save()
            
            file2 = FileEntry(
                archive = archive1,
                filepath = '/some/authenti',
                filename = 'test2',
                extension = 'jpg',
                filesize = 1000,
                created_datetime = datetime.datetime.today(),
                modified_datetime = datetime.datetime.today(),
                authentication_required = True,
                mature_content = False,
                personal_content = False,
                in_the_attic = False
            )
            file2.save()
            
            file3 = FileEntry(
                archive = archive1,
                filepath = '/some/mature',
                filename = 'test3',
                extension = 'jpg',
                filesize = 1000,
                created_datetime = datetime.datetime.today(),
                modified_datetime = datetime.datetime.today(),
                authentication_required = False,
                mature_content = True,
                personal_content = False,
                in_the_attic = False
            )
            file3.save()
            
            file4 = FileEntry(
                archive = archive1,
                filepath = '/some/personal',
                filename = 'test4',
                extension = 'jpg',
                filesize = 1000,
                created_datetime = datetime.datetime.today(),
                modified_datetime = datetime.datetime.today(),
                authentication_required = False,
                mature_content = False,
                personal_content = True,
                in_the_attic = False
            )
            file4.save()
            
            file5 = FileEntry(
                archive = archive1,
                filepath = '/some/attic',
                filename = 'test5',
                extension = 'jpg',
                filesize = 1000,
                created_datetime = datetime.datetime.today(),
                modified_datetime = datetime.datetime.today(),
                authentication_required = False,
                mature_content = False,
                personal_content = False,
                in_the_attic = True
            )
            file5.save()

            self.assertEqual(len(FileEntry.objects.all()), 5)
            self.assertEqual(len(FileEntry.get_file_entries(user_fred)), 3)
            self.assertEqual(len(FileEntry.get_file_entries(user_fred, True)), 4)
            self.assertEqual(len(FileEntry.get_file_entries(user_cecilie)), 2)
            self.assertEqual(len(FileEntry.get_file_entries(None)), 1)
            #self.assertRaises(KeyError, random.sample, self.seq, 20)    
        except Exception, e:
            print "Test failed. Reason: ", e
        finally:
            file1.delete()
            file2.delete()
            file3.delete()
            file4.delete()
            file5.delete()
            archive1.delete()
            user_fred.delete()
            user_cecilie.delete()

    def testBar(self):        
        self.failUnless(self.foo == 1, 'Woopsidaisy')

#def suite():
#    suite = unittest.TestLoader()#unittest.TestSuite()
#    suite.addTest(unittest.makeSuite(testPrimitiveArchive))
#    return suite
#
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(testPrimitiveArchive)
    unittest.TextTestRunner(verbosity=2).run(suite)    
    #unittest.main()
